
var _firstNavUl = $(".nav").children('ul');

$( function(){

	// 主导航栏的动作

	var _firstNavLi = _firstNavUl.children('li');

	_firstNavLi.on('mouseenter', function(event) {
		event.preventDefault();
		/* Act on the event */

		$('.nav .sub-nav').finish();
		//结束当前正在进行的动画

		var _subNav = $(this).find(".sub-nav");

		_subNav.slideDown(500);

		setSubNavPosition(_subNav);

	}).on('mouseleave', function(event) {
		event.preventDefault();
		/* Act on the event */

		$(this).find('.sub-nav').slideUp(500);
	});


	//首页, 教育培训，科研服务背景图片
	var _iconLeft = $('.blockIconList .icon-left');
	var _iconRight = $('.blockIconList .icon-right');

	var _leftPics = ['student2' , 'Planet', 'Like', 'Volume', 'Pulse', 'Folder'];
	var _rightPics = ['Document', 'Messages', 'Pc', 'Award', 'Share', 'Sun'];

	setIconBg(_iconLeft, _leftPics);
	setIconBg(_iconRight, _rightPics);

})

// 获取主导航的下拉导航的宽度
var getSubNavWidth = function( subNavUl ){

	var subNavLi = subNavUl.find('li');

	var widthSum = 0;

	subNavLi.each(function(index, el) {

		widthSum = widthSum + $(this).outerWidth() ;
	});
	return widthSum;
};


var setSubNavPosition = function(_subNavUl ){

	var _subNavWidth = getSubNavWidth(_subNavUl );
	var _subNavParent = _subNavUl.parent("li");

	var _ParentOffsetLeft = _subNavParent.offset().left;

	var _subPadLeft = _ParentOffsetLeft + _subNavParent.outerWidth()/2 -  _subNavWidth/2;

	var min_padleft = _firstNavUl.offset().left;

	var max_padleft = min_padleft + _firstNavUl.width() - _subNavWidth;

	if (_subPadLeft < min_padleft) _subPadLeft = min_padleft;

	if (_subPadLeft > max_padleft) _subPadLeft = max_padleft;

	_subNavUl.css("padding-left", _subPadLeft );
}

 // 首页， blockIconLIst 背景图片切换
 var setIconBg = function ( iconContainer, picsName) {

 	var iconUl = iconContainer.find('ul li a');

 	iconUl.each(function(index) {

 		var _thisIcon = $(this);
 		var iconUrl = '../images/' + picsName[index] + '.png';
 		var iconUrlActive = '../images/' + picsName[index] + '_active.png';

 		_thisIcon.css({
 			background: '#e7e7e7 url(' + iconUrl + ') no-repeat 50% 35%'
 		});

 		_thisIcon.mouseenter(function(event) {
 			$(this).css({
				background: '#a2494f url(' + iconUrlActive + ') no-repeat 50% 35%',
				color: '#fff'
 			});
 		}).mouseleave(function(event) {
  			$(this).css({
 				background: '#e7e7e7 url(' + iconUrl + ') no-repeat 50% 35%',
 				color: '#000'
  			});
 		});
 	});
 }

